<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="/misc/css/base.css"  />
<link rel="stylesheet" type="text/css" href="/misc/css/btnicon.css"  />
<link rel="stylesheet" type="text/css" href="/misc/css/reset_admin.css"  />
<link rel="stylesheet" type="text/css" href="/misc/css/adminmenu.css"  />
<link rel="stylesheet" type="text/css" href="/misc/css/newadmin.css"  />
<script type="text/javascript" src="/misc/vendors/jquery/jquery_1.2.6.js"></script>
<script type="text/javascript" src="/misc/vendors/o2js/o2js.js"></script>
<script type="text/javascript" src="/misc/vendors/o2js/o2js_build.js"></script>
<script type="text/javascript" >
var baseUrl = "<?=$themeUrl?>";
$().ready(function(){

});
var grids;
</script>
</head>
<body   style="background-color:#fcfcfc;"> 



<style type="text/css">
td.mylabel{
width:90px!important;
}




/* 配送会员选取 */
a.selectinfo_a{
display:block;
padding:3px;
border:1px solid #B2D9F0;
color:#0082CB;
margin:1px;
text-decoration:none;
}
a.selectinfo_a:hover{
text-decoration:line-through;
color:#666666;
background-color:#FFE8E8
}

/* 配送蔬菜选择表格样式 */
table.vegetable{
border-collapse:collapse;
width:325px;
}
table.vegetable th{
background-color:#FFFFCC;
color:#666666;
padding:5px;
border:1px solid #CCCCCC;
}
table.vegetable td{
background-color:#fcfcfc;
color:#333333;
padding:5px;
border:1px solid #CCCCCC;
} 
input.vegetext{
width:50px
}


/* 固定蔬菜表格 */
#fixvege_div{
height:250px;
border:1px solid #CCCCCC;
overflow-y:scroll;
overflow-x:hidden;
padding:1px
;margin:10px auto;
width:345px;
}



/* 可选蔬菜表格 */
#sltvege_div{
height:250px;
border:1px solid #CCCCCC;
overflow-y:scroll;
overflow-x:hidden;
padding:1px;
margin:10px auto;
width:345px
}




</style>	
<script type="text/javascript">
var url_save = '/admin_sendfen/add';
var rs_sendpi = {json_encode($rs_sendpi)};
var rs_veges = {json_encode($rs_veges)};
$().ready(function(){  
	showLoading();  
  $('#name').focus();
});

/* 保存 */
function fnSave(){
	var rsfix = [];
	var rsslt = [];
	var rsuser = [];
	$('#fix_tbody .fixvege_tr').each(function(){
		var myid = $(this).attr('myid');
		var vege_num =  parseInt(strToNum($('#fix_vegenum_'+myid).val()),10);
		if(vege_num <= 0){
			vege_num = 0;
		}
		$('#fix_vegenum_'+myid).val(vege_num);
		rsfix.push({
			vege_id:myid,
			vege_num:vege_num,
			"fixed":"1"
		});
	});
	$('#slt_tbody .sltvege_tr').each(function(){
		var myid = $(this).attr('myid');
		var vege_num =  parseInt(strToNum($('#slt_vegenum_'+myid).val()),10);
		if(vege_num <= 0){
			vege_num = 0;
		}
		$('#slt_vegenum_'+myid).val(vege_num);
		rsslt.push({
			vege_id:myid,
			vege_num:vege_num,
			"fixed":"0"
		});
	});
	$('#selectuser_div').find('.selectinfo_a').each(function(){
		rsuser.push($(this).attr('myid'));
	});
	fnSetSltnum();
	var form = {
		name:$('#name').val(),
		sendpi_id:rs_sendpi.id,
		sltnum:$('#sltnum').val()
	};
	/* 验证 */
	if($.trim(form.name) == ''){
		alert('请输入【配送分支标识】');
		$('#name').focus();
		return false;
	}
	if(rsuser.length==0){
		alert('请选择配送会员');
		return false;
	}
//	if(rsfix.length==0){
//		alert('请选择主选菜品');
//		return false;
//	}
	if(rsfix.length==0 && rsslt.length==0){
		alert('请选择菜品');
		return false;
	}
	for(var ii=0; ii<rsfix.length; ii++){
		if(rsfix[ii].vege_num == 0){
			alert('主选菜品份数必须大于0');
			return false;
		}
	}
	if(rsslt.length!=0){
		if(form.sltnum <= 0){
			alert('请至少填写大于1的默认份数');
			return false;
		}
		
		//判断是否超过最大可选项
		for(var ii=0; ii<rsslt.length; ii++){
			var obj = rsslt[ii];
			if(rs_veges[obj.vege_id].maxnum != '' && rs_veges[obj.vege_id].maxnum>0 && obj.vege_num > rs_veges[obj.vege_id].maxnum){
				alert('菜品【'+rs_veges[obj.vege_id].name+'】的最大可选份数为'+rs_veges[obj.vege_id].maxnum+'，请填写小于等于'+rs_veges[obj.vege_id].maxnum+'的默认份数');
				return false;
			}	
		}
	}
 
	
  $('button').attr('disabled','disabled');
	$.ajax({
		async:true,
		type:'post',
		url:url_save,
		data:{
			rs_sendpi:JSON.encode(rs_sendpi),
			form:JSON.encode(form),
			rsfix:JSON.encode(rsfix),
			rsslt:JSON.encode(rsslt),
			rsuser:JSON.encode(rsuser)
		},
		success:function(e){
  		$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok){　 
				<?PHP
				if(isset($_GET['endfunc'])){
					echo 'parent.'.$_GET['endfunc'].'(ok);';
				}
				?>
				fnReturn();
			}
		}
	});
}
/* 返回 */
function fnReturn(){
	if(defined(parent) && defined(parent.ymPrompt)){
		parent.ymPrompt.close();
	}else{
		window.close();
	}
}


/* 选择用户 */
function fnSltUser(){
	GfnSelectUser('onFnSltUser','选取配送会员');
}
function onFnSltUser(e){
	var rsuser = [];
	$('#selectuser_div').find('.selectinfo_a').each(function(){
		rsuser.push($(this).attr('myid'));
	});
	var html='';
	for(var i=0; i<e.user_ids.length ; i++){
		var addma = true;
		for(var k=0; k<rsuser.length ; k++){
			if(e.user_ids[i].id == rsuser[k]){
				addma = false;
			}
		}
		if(addma){
			html += '<a class="selectinfo_a" href="javascript:void(0)" myid="'+e.user_ids[i].id+'" title="点击删除所选" onclick="fnRemoveUser('+e.user_ids[i].id+')">'+e.user_ids[i].username+'('+e.user_ids[i].name+')</a>';
		}	
	}
	if(html != ''){
		$("#selectuser_div").append(html);
	}	
} 
function fnRemoveUser(id){
	$("#selectuser_div").find('a.selectinfo_a').each(function(e){
		if($(this).attr('myid') == id){
			$(this).remove();
		}
	});
}
function fnRemoveAllUser(){
	$("#selectuser_div").html('');
}


var fix_veges = {json_encode($fix_veges)};
var slt_veges = {json_encode($slt_veges)};
$().ready(function(){
	onFnSltCaiFix(fix_veges);
	onfnSltCaiSlt(slt_veges);
});

/* 主选菜品选取 */
function fnSltCaiFix(){
	GfnSelectVege('onFnSltCaiFix','主选菜品选取');
}
function onFnSltCaiFix(e){
	var rsvege = [];
	$('#fix_tbody .fixvege_tr').each(function(){
		rsvege.push($(this).attr('myid'));
	});
	
	var html='';
	for(var i=0; i<e.vege_ids.length ; i++){ 
		var addma = true;
		for(var k=0; k<rsvege.length ; k++){
			if(e.vege_ids[i].id == rsvege[k]){
				addma = false;
			}
		}
		if(addma){
			html += '<tr class="fixvege_tr" id="fixvege_tr_'+e.vege_ids[i].id+'" myid="'+e.vege_ids[i].id+'">';
			html += '	<td>';
			html += e.vege_ids[i].name;
			html += '	</td>';
			html += '	<td>';
			html += '		<input type="text" class="vegetext"  id="fix_vegenum_'+e.vege_ids[i].id+'" value="1" />';
			html += '	</td>';
			html += '	<td><a href="javascript:void(0)" onclick="fnRemoveFix('+e.vege_ids[i].id+')" title="点击删除本行">删除</a></td>';
			html += '</tr>';
		}
	}
	if(html != ''){
		$("#fix_tbody").append(html);
	}	
} 
function fnRemoveFix(id){
	$("#fixvege_tr_"+id).remove();
}
function fnRemoveAllFix(){
	$("#fix_tbody").html('');
}


 
/* 自选菜品的总份数 */
function fnSetSltnum(){
	var allnum = 0;
	$('.slt_vegenum').each(function(){
		var v = $(this).val();
		allnum += strToNum(v);
	});
	$('#sltnum').val(allnum);
}
/* 自选菜品 */
function fnSltCaiSlt(){
	GfnSelectVege('onfnSltCaiSlt','自选菜品选取');
}
function onfnSltCaiSlt(e){
	var rsvege = [];
	$('#slt_tbody .sltvege_tr').each(function(){
		rsvege.push($(this).attr('myid'));
	});
	
	var html='';
	for(var i=0; i<e.vege_ids.length ; i++){
		var addma = true;
		for(var k=0; k<rsvege.length ; k++){
			if(e.vege_ids[i].id == rsvege[k]){
				addma = false;
			}
		}
		if(addma){
			html += '<tr class="sltvege_tr" id="sltvege_tr_'+e.vege_ids[i].id+'" myid="'+e.vege_ids[i].id+'">';
			html += '	<td>';
			html += e.vege_ids[i].name;
			html += '	</td>';
			html += '	<td>';
			html += '		<input type="text" class="vegetext slt_vegenum"  id="slt_vegenum_'+e.vege_ids[i].id+'" value="1"  onchange="fnSetSltnum()"  />';
			html += '	</td>';
			html += '	<td><a href="javascript:void(0)" onclick="fnRemoveSlt('+e.vege_ids[i].id+')" title="点击删除本行">删除</a></td>';
			html += '</tr>';	
		}		
	}
	if(html != ''){
		$("#slt_tbody").append(html);
	}	
	fnSetSltnum();
} 
function fnRemoveSlt(id){
	$("#sltvege_tr_"+id).remove();
	fnSetSltnum();
}
function fnRemoveAllSlt(){
	$("#slt_tbody").html('');
	fnSetSltnum();
}






</script>
 
<div style="margin:5px;">
	<div class="blockdiv">
		<div class="titlediv">配送分支信息</div>
		<table class="mytable" > 
			<tr>
				<td class="mylabel"><span class="red">*</span>配送分支标识：</td>
				<td class="myinfo" >
					<input type="text" class="textBox" id="name" name="name" maxlength="100" style="width:200px" />
				</td>  
			</tr>
		</table> 
	</div>
</div>

 
<div style="margin:5px;">
	<div class="blockdiv">
		<div class="titlediv">配送会员</div> 
		<table class="mytable" >
			<tr>
				<td class="mylabel">配送会员：</td>
				<td class="myinfo" >
					<a href="javascript:void(0)" onclick="fnSltUser()">选取会员</a>
					<a href="javascript:void(0)" onclick="fnRemoveAllUser()" style="color:red;margin-left:30px" >清空</a>
				</td>  
			</tr> 
			<tr>
				<td class="mylabel"> </td>
				<td class="myinfo" >
					<div id="selectuser_div" style="height:200px;border:1px solid #CCCCCC;overflow-y:auto;overflow-x:hidden;padding:1px;margin:10px auto"> 
					</div>
				</td>  
			</tr>  
		</table>
	</div>
</div>
<div style="margin:5px;">
	<div class="blockdiv">
		<div class="titlediv">配送菜品</div>
		<table>
			<tR>
				<TD style="width:48%;text-align:left;vertical-align:top">
						<table class="mytable" >
							<tr>
								<td class="mylabel">主选菜品：</td>
								<td class="myinfo"   >
									<a href="javascript:void(0)" onclick="fnSltCaiFix()">选取菜品</a>
									<a href="javascript:void(0)" onclick="fnRemoveAllFix()" style="color:red;margin-left:30px" >清空</a>
								</td>  
							</tr>
							<tr>
								<td class="mylabel">  </td>
								<td class="myinfo" >
									<div id="fixvege_div"  > 
										<table class="vegetable">
											<thead>
												<tr>
													<th>菜品</th>
													<th style="width:50px;">份数</th>
													<th style="width:50px;"> </th>
												</tr>
											</thead>	
											<tbody id="fix_tbody">
	
											</tbody>
										</table>
									</div>
								</td>  
							</tr> 
						</table>	
				</TD>
				<TD style="width:48%;text-align:left;vertical-align:top">
						<table class="mytable" > 
							<tr>
								<td class="mylabel">自选菜品：</td>
								<td class="myinfo"   >
									<a href="javascript:void(0)" onclick="fnSltCaiSlt()">选取菜品</a>
									<a href="javascript:void(0)" onclick="fnRemoveAllSlt()" style="color:red;margin-left:30px" >清空</a>
								</td>  
							</tr>
							<tr>
								<td class="mylabel">  </td>
								<td class="myinfo" >
									<div id="sltvege_div" > 
										<table class="vegetable">
											<thead>
												<tr>
													<th>菜品</th>
													<th style="width:50px;">默认份数</th>
													<th style="width:50px;"> </th>
												</tr>
											</thead>	
											<tbody id="slt_tbody">
	
											</tbody>
										</table>
									</div>
								</td>  
							</tr>
							<tr>
								<td class="mylabel">自选总份数</td>
								<td class="myinfo" >
									 <input type="text"  id="sltnum" name="sltnum" value="0" maxlength="10" readonly="readonly" class="textBoxOff" style="width:50px;background-color:#CCCCCC"  /> 
									 <span style="color:#666666">由您填写的自选菜品 默认份数 决定</span>
								</td>  
							</tr>
						</table>
				</TD>
			</tR>
		</table>
		 
	</div>	
</div>	
<div style="margin:5px;">		
	<div  class="button_div">
			<button class="btn_save" onclick="fnSave()">保 存</button>
			<span class="gray">或</span> 
			<a href="javascript:fnReturn()">取消</a>
	</div>  
</div>	




		 
</body>
</html>